package com.ruoyi.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.rabbitmq.RabbitMQConsumerService;
import com.ruoyi.system.domain.SysNotification;
import com.ruoyi.system.domain.UserNotification;
import com.ruoyi.system.domain.vo.UserNotificationUnReadVo;
import com.ruoyi.system.mapper.UserNotificationMapper;
import com.ruoyi.system.service.ISysNotificationService;
import com.ruoyi.system.service.ISysUserService;
import com.ruoyi.system.service.IUserNotificationService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 用户通知表Service业务层处理
 *
 * @author xudengyu
 * @date 2025-03-09
 */
@Service
public class UserNotificationServiceImpl implements IUserNotificationService {
    public static final String NO_READ = "0";
    public static final String IS_READ = "1";

    @Resource
    private UserNotificationMapper userNotificationMapper;

    @Resource
    private ISysNotificationService sysNotificationService;
    @Resource
    private RabbitMQConsumerService rabbitMQConsumerService;

    /**
     * 查询用户通知表
     *
     * @param userNotificationId 用户通知表主键
     * @return 用户通知表
     */
    @Override
    public UserNotification selectUserNotificationByUserNotificationId(Long userNotificationId) {
        return userNotificationMapper.selectUserNotificationByUserNotificationId(userNotificationId);
    }

    /**
     * 查询用户通知表列表
     *
     * @param userNotification 用户通知表
     * @return 用户通知表
     */
    @Override
    public List<UserNotification> selectUserNotificationList(UserNotification userNotification) {
        return userNotificationMapper.selectUserNotificationList(userNotification);
    }

    /**
     * 新增用户通知表
     *
     * @param userNotification 用户通知表
     * @return 结果
     */
    @Override
    public int insertUserNotification(UserNotification userNotification) {
        return userNotificationMapper.insertUserNotification(userNotification);
    }

    /**
     * 修改用户通知表
     *
     * @param userNotification 用户通知表
     * @return 结果
     */
    @Override
    public int updateUserNotification(UserNotification userNotification) {
        return userNotificationMapper.updateUserNotification(userNotification);
    }

    /**
     * 批量删除用户通知表
     *
     * @param userNotificationIds 需要删除的用户通知表主键
     * @return 结果
     */
    @Override
    public int deleteUserNotificationByUserNotificationIds(Long[] userNotificationIds) {
        return userNotificationMapper.deleteUserNotificationByUserNotificationIds(userNotificationIds);
    }

    /**
     * 删除用户通知表信息
     *
     * @param userNotificationId 用户通知表主键
     * @return 结果
     */
    @Override
    public int deleteUserNotificationByUserNotificationId(Long userNotificationId) {
        return userNotificationMapper.deleteUserNotificationByUserNotificationId(userNotificationId);
    }

    @Override
    public List<UserNotificationUnReadVo> getUnread(Long userId) {
        rabbitMQConsumerService.addQueueListener("user." + SecurityUtils.getUserId() + ".queue", SecurityUtils.getUserId().toString());
        List<UserNotification> userNotificationList = selectUserNotificationList(
                new UserNotification().setUserId(userId)).stream()
                .filter(i -> NO_READ.equals(i.getIsRead()))
                .collect(Collectors.toList());
        ArrayList<UserNotificationUnReadVo> res = new ArrayList<>();
        userNotificationList.forEach(i -> {
            SysNotification sysNotification = sysNotificationService.selectSysNotificationByNotificationId(i.getNotificationId());
            UserNotificationUnReadVo userNotificationUnReadVo = BeanUtil.copyProperties(
                    sysNotification,
                    UserNotificationUnReadVo.class
            );
//            userNotificationUnReadVo.setNotificationPuberName(
//                    sysUserService.selectUserById(sysNotification.getNotificationPuberId()).getUserName()
//            );
            userNotificationUnReadVo.setUserNotificationId(i.getUserNotificationId());
            res.add(userNotificationUnReadVo);
        });
        return res;
    }
}
